package edu.spec.teacher.controller;

import edu.spec.basic.model.User;
import edu.spec.basic.model.dto.MvcReturn;
import edu.spec.basic.model.dto.UserDto;
import edu.spec.basic.model.enumeration.FinishStatus;
import edu.spec.basic.service.iface.IUserService;
import edu.spec.teacher.model.Elect;
import edu.spec.teacher.model.UserElect;
import edu.spec.teacher.service.iface.IElectService;
import edu.spec.teacher.service.iface.IUserElectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/10/15 11:15
 * Description  :
 */
@Controller
@RequestMapping("/teacher/userElect")
public class UserElectController {

    @Autowired
    private IUserElectService userElectService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IElectService electService;

    @ResponseBody
    @RequestMapping("/listEffective")
    public ModelAndView listEffective(HttpSession session){
        User u = (User)session.getAttribute("user");
        List<UserElect> userElectList = userElectService.listEffectiveByUser(u.getId());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userElectList", userElectList);
        modelAndView.setViewName("teacher/userElectList");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/vote/{userElectId}")
    public ModelAndView go2VoteView(@PathVariable Integer userElectId,  HttpSession session){
        UserElect userElect = userElectService.load(userElectId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userElect", userElect);
        modelAndView.setViewName("teacher/vote");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/vote")
    public MvcReturn vote(Integer userElectId, String result){
        UserElect userElect = userElectService.load(userElectId);
        userElect.setResult(result);
        userElect.setFinishDate(new Date());
        userElect.setFinishStatus(FinishStatus.FINISHED);
        userElectService.update(userElect);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/listCandidate/{electId}")
    public MvcReturn listCandidate(@PathVariable Integer electId){
        Elect elect = electService.load(electId);
        return new MvcReturn(true, userService.listUserDtoByIdString(elect.getCandidate()));
    }

    private List<UserDto> convert(List<User> userList){
        List<UserDto> udList = new ArrayList<UserDto>();
        for (User u : userList){
            udList.add(new UserDto(u.getId(), u.getRealName()));
        }
        return udList;
    }

}
